#include<iostream>
#include<cstring>
using namespace std;
const int N = 1010;
int p[N];
int n, m;
bool st[N][N];
int find(int x)
{
	if (x != p[x]) {
		p[x] = find(p[x]);
	}
	return p[x];
}
void init()
{
	for (int i = 1; i <= n; i++) {
		p[i] = i;
	}
}
int main()
{
	while (scanf("%d",&n)) {
		if (!n) {
			break;
		}
		scanf("%d", &m);
		memset(p, 0, sizeof(p));
		memset(st, 0, sizeof(st));
		int a, b;
		init();
		while (m--) {
			scanf("%d%d", &a, &b);
			if (st[a][b]) {
				continue;
			}
			else {
				a = find(a);
				b = find(b);
				p[a] = b;
				st[a][b] = st[b][a];
			}
		}
		int ans = 0;
		for (int i = 1; i <= n; i++) {
			if (i == find(i)) {
				ans++;
			}
		}
		cout << ans - 1 << endl;
	}
	return 0;
}